﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary.DAO;

public partial class Admin_ucControl_ucListItem : System.Web.UI.UserControl
{
    TblItemsDAO itemDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblMenuChildDAO menuChildDAO = new TblMenuChildDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadItems();
            LoadMenuChild();
        }
    }

    private void LoadMenuChild()
    {
        drCategory.DataSource = menuChildDAO.GetAllMenuChild();
        drCategory.DataTextField = "MenuChildName";
        drCategory.DataValueField = "IDMenuChild";
        drCategory.DataBind();
    }
    private void LoadItems()
    {
        grdItems.DataSource = itemDAO.GetItems_All();
        grdItems.DataBind();
    }

    private void LoadData(string strName, string strCatID, string strOrderby)
    {
        grdItems.DataSource = itemDAO.GetListItems(0, strName, strCatID, '1', 0, 100000, "");
        grdItems.DataBind();
    }
    protected void btnSearch_1_Onclick(object sender, EventArgs e)
    {
        LoadData(txtName.Text, drCategory.SelectedValue.ToString(), "");
    }
    protected void btnDel_Onclick(object sender, EventArgs e)
    {
        foreach (GridViewRow grd in grdItems.Rows)
        {
            CheckBox cbDel = (CheckBox)grd.FindControl("checkGia");
            bool delete = false;
            try
            {
                if (cbDel.Checked == true)
                {
                    HiddenField hidField1 = (HiddenField)grd.FindControl("hidField1");
                    string ItemID = hidField1.Value;
                    itemDAO.DeleteItem(Global.ConvertToInt(ItemID));
                }
                delete = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (delete)
            {
                LoadData(txtName.Text, Convert.ToString(drCategory.SelectedValue.ToString()), "");
            }
        }

        LoadData(txtName.Text, Convert.ToString(drCategory.SelectedValue.ToString()), "");
    }
    protected void checkGia_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow grd in grdItems.Rows)
        {
            TextBox txtGia = (TextBox)grd.FindControl("txtGia");
            CheckBox checkGia = (CheckBox)grd.FindControl("checkGia");

            if (checkGia.Checked == true)
            {
                txtGia.Enabled = true;
            }
            else
                txtGia.Enabled = false;
        }
    }
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        bool flag = false;
        try
        {
            int ItemID = Convert.ToInt32(((ImageButton)sender).CommandArgument);

            string t = "";
            ImageButton btnConfirm = (ImageButton)sender;
            string id = btnConfirm.UniqueID;
            int iRowIndex = (Convert.ToInt16(id.Split('$')[4].Replace("ctl", "")) - 2);

            GridViewRow gridRow = grdItems.Rows[iRowIndex];

            HiddenField hidField1 = (HiddenField)gridRow.FindControl("hidField1");
            t = hidField1.Value;
            TextBox txtGia = (TextBox)gridRow.FindControl("txtGia");
            itemDAO.UpdatePrice(ItemID, Convert.ToDouble(txtGia.Text));

        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/Default.aspx");
        }
    }
    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        int ItemID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
        Response.Redirect("~/Admin/ThemSanPham.aspx?id=" + ItemID);
    }
    protected void btnConfirm_Click(object sender, ImageClickEventArgs e)
    {
        bool flag = false;
        try
        {
            int ItemID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            itemDAO.DeleteItem(ItemID);
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
            Response.Redirect("~/Admin/List-Product.aspx");
    }
    protected void grdItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdItems.PageIndex = e.NewPageIndex;
        LoadItems();
    }
    protected void grdItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        //button.CommandArgument = ((TblEmail)e.Row.DataItem).IDEmail.ToString();
                        button.OnClientClick = "if (!confirm('Bạn có thực sự muốn xoá sản phẩm này ?')) return false;";
                    }
                    if (button != null && button.CommandName == "Save")
                    {
                        // Add delete confirmation
                        //button.CommandArgument = ((TblEmail)e.Row.DataItem).IDEmail.ToString();
                        button.OnClientClick = "if (!confirm('Bạn có muốn lưu sản phẩm này ?')) return false;";
                    }
                }
            }
            HiddenField hidField1 = (HiddenField)e.Row.Cells[3].FindControl("hidField1");
            hidField1.Value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ItemID"));

            TextBox txtGia = (TextBox)e.Row.Cells[3].FindControl("txtGia");
            CheckBox checkGia = (CheckBox)e.Row.Cells[3].FindControl("checkGia");
            decimal price;
            price = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Price"));
            txtGia.Text = price.ToString("0,00.##");

            if (checkGia.Checked == true)
            {
                txtGia.Enabled = true;

            }
            else
                txtGia.Enabled = false;
        }
    }
}